package com.company.cloud.page.modules.app.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.cloud.common.auth.annotation.Login;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.tree.CloudTree;
import com.company.cloud.page.modules.base.entity.PageStaticInfo;
import com.company.cloud.page.modules.base.service.PageStaticInfoService;
import com.company.cloud.page.modules.cache.models.PageStaticModel;
import com.company.cloud.page.modules.cache.utils.PageConvertUtils;
import com.company.cloud.page.modules.mgr.controller.req.PageConfigQuery4PageParam;
import com.company.cloud.page.modules.mgr.controller.req.PageStaticTreeParam;
import com.company.cloud.page.modules.mgr.service.PageConfigMgrService;
import com.company.common.server.config.cloud.ProjectConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.redis.service.RedisUtils;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.page.modules.app.controller.req.PageDataQuery4PageParam;
import com.company.cloud.page.modules.app.service.PageDataService;
import com.company.cloud.page.modules.base.entity.PageConfigInfo;
import com.company.cloud.page.modules.cache.models.PageInfoModel;
import com.company.cloud.page.modules.cache.service.PageCacheService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

@RestController
@RequestMapping("/app/cms/page")
@Api(tags = "Page_App_页面(数据)信息")
public class PageController {

	@Autowired
	private PageCacheService pageCacheService;
	@Autowired
	private PageDataService pageDataService;
	@Autowired
	private RedisUtils redisUtils;
	@Autowired
	private PageConfigMgrService pageConfigMgrService;
	@Autowired
	private ProjectConfig projectConfig;
	@Autowired
	private PageStaticInfoService pageStaticInfoService;


	@Login
	@PostMapping("/queryByCompany")
	@ApiOperation(value="公司页面列表查询[login]", response= PageInfoModel.class)
	public BaseResult queryByCompany(@RequestBody PageConfigQuery4PageParam params) {
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		params.setCompanyNo(userInfo.getCompanyNo());
		params.setContent(false);
		IPage<PageInfoModel> data=pageConfigMgrService.query(params);
		return BaseResult.ok(data);
	}

	@PostMapping("/queryByTemplate")
	@ApiOperation(value="页面模板列表查询", response= PageInfoModel.class)
	public BaseResult queryByTemplate(@RequestBody PageConfigQuery4PageParam params) {
		params.setContent(false);
		params.setIsTemplate(BaseConstant.YNEnum.YES.getCode());
		IPage<PageInfoModel> data=pageConfigMgrService.query(params);
		return BaseResult.ok(data);
	}

	@GetMapping("/getPage")
	@ApiOperation(value = "页面详细信息(无数据)", response = PageConfigInfo.class)
	public BaseResult getPage(@RequestParam("pageNo") String pageNo) {
		if (StringUtils.isEmpty(pageNo)) {
			throw new BusinessException(BaseReturnEnum.PARAM_NULL);
		}
		PageInfoModel data = pageCacheService.getPageConfigCache(pageNo);
		return BaseResult.ok(data);
	}

	@GetMapping("/getPageByPosition")
	@ApiOperation(value = "根据位置取页面详细信息(无数据)", response = PageConfigInfo.class)
	public BaseResult getPageByPosition(@RequestParam("pagePositionNo") String pagePositionNo) {
		if (StringUtils.isEmpty(pagePositionNo)) {
			throw new BusinessException(BaseReturnEnum.PARAM_NULL);
		}
		String companyNo;
		if(projectConfig.isPlatformPattern()){
			companyNo=BaseConstant.Platform_Company;
		}else{
			LoginSimpleUserInfo userInfo = AuthContextUtils.getUserNoValidate();
			if(userInfo!=null){
				companyNo=userInfo.getCompanyNo();
			}
			companyNo=AuthContextUtils.getCompanyNo();
		}
		PageInfoModel data = pageCacheService.getPageConfigCache(companyNo,pagePositionNo);
		return BaseResult.ok(data);
	}

	@GetMapping("/getPage2Data")
	@ApiOperation(value = "页面数据和信息", response = PageInfoModel.class)
	public BaseResult getPage2Data(@RequestParam("pageNo") String pageNo) {
		if (StringUtils.isEmpty(pageNo)) {
			throw new BusinessException(BaseReturnEnum.PARAM_NULL);
		}
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUserNoValidate();
		PageInfoModel data = pageCacheService.getPageConfig2DataByCache(pageNo,userInfo);
		return BaseResult.ok(data);
	}


	@GetMapping("/getPage2DataByPosition")
	@ApiOperation(value = "根据位置取页面数据和信息", response = PageInfoModel.class)
	public BaseResult getPage2DataByPosition(@RequestParam("pagePositionNo") String pagePositionNo) {
		if (StringUtils.isEmpty(pagePositionNo)) {
			throw new BusinessException(BaseReturnEnum.PARAM_NULL);
		}
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUserNoValidate();
		String companyNo;
		if(projectConfig.isPlatformPattern()){
			companyNo=BaseConstant.Platform_Company;
		}else{
			if(userInfo!=null){
				companyNo=userInfo.getCompanyNo();
			}
			companyNo=AuthContextUtils.getCompanyNo();
		}
		PageInfoModel data = pageCacheService.getPageConfig2DataByCache(companyNo,pagePositionNo,userInfo);
		return BaseResult.ok(data);
	}

	@PostMapping("/queryPageData")
	@ApiOperation(value = "页面数据查询", response = BaseResult.class)
	public BaseResult queryPageData(@RequestBody PageDataQuery4PageParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUserNoValidate();
		Object data = pageDataService.queryPageEleData(params, userInfo);
		return BaseResult.ok(data);
	}



	@PostMapping("/tree")
	@ApiOperation(value = "静态页面树", response = PageStaticModel.class)
	public BaseResult tree(@RequestBody PageStaticTreeParam params) {
		List<PageStaticInfo> list = pageStaticInfoService.list(new QueryWrapper<PageStaticInfo>()
				.eq("rows_state", BaseConstant.EnabledEnum.ENABLED.getCode())
				.eq(StrUtil.isNotEmpty(params.getType()),"type",params.getType())
				.orderByDesc("orders")
		);
		if(CollUtil.isEmpty(list)){
			return BaseResult.ok();
		}
		CloudTree<PageStaticInfo, PageStaticModel> tree=new CloudTree<PageStaticInfo, PageStaticModel>() {
			@Override
			public boolean isTop(PageStaticModel entity) {
				if(StrUtil.isEmpty(entity.getParentPageNo()) || "0".equals(entity.getParentPageNo())) {
					return true;
				}else {
					return false;
				}
			}
			@Override
			public boolean isParent(PageStaticModel parentObj, PageStaticModel childObj) {
				if(parentObj.getPageNo().equals(childObj.getParentPageNo())){
					return true;
				}else{
					return false;
				}
			}
			@Override
			public PageStaticModel convert(PageStaticInfo entity) {
				return PageConvertUtils.getPageStaticModel(entity);
			}
		};
		return BaseResult.ok(tree.tree(list));
	}


	@GetMapping("/staticPage")
	@ApiOperation(value = "获取静态页面", response = PageStaticModel.class)
	public BaseResult staticPage(@RequestParam("pageNo") String pageNo) {
		PageStaticInfo entity= pageStaticInfoService.getById(pageNo);
		if(entity==null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())){
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		PageStaticModel data= PageConvertUtils.getPageStaticModel(entity);
		return BaseResult.ok(data);
	}



}
